<template>
  <div class="body">
    <!-- 搜素框 1000x60-->
    <div class="search__header">

    </div>

    <!-- <SearchHeader /> -->

    <!-- 身体部分1000x4394 -->

    <div class="grid-total">
      <div class="grid-left">
        <div class="mainsrp-tab">
          <div class="nav-box">
            <div>所有宝贝</div>
            <div>天猫</div>
            <div>二手</div>
          </div>
        </div>
        <div class="mainsrp-nav">
          <div class="show-icon">
            <span>所有分类></span>
            <button>显示帅选icon</button>
          </div>
          <div class="groups-item">
            <div class="attr-box">
              <span class="title">品牌</span>
              <!-- <div class="box">
                <span>七匹狼</span>
                <span>七匹狼</span>
                <span>七匹狼</span>
                <span>七匹狼</span>
                <span>七匹狼</span>
                <span>七匹狼</span>
                <span>七匹狼</span>
                <span>七匹狼</span>
                <span>七匹狼</span>
                <span>七匹狼</span>
                <span>七匹狼</span>
                <span>七匹狼</span>
                <span>七匹狼</span>
                <span>七匹狼</span>
                <span>七匹狼</span>
                <span>七匹狼</span>
              </div> -->
              <div class="box">
                <span v-for="item in trademarkList" @click="trademarkSearch(item)">{{ item.tmName }}</span>
              </div>
              <div class="check">
                <div class="check-some">多选</div>
                <div class="more">更多</div>
              </div>
            </div>
            <div class="attr-box" v-for="item in attrsList">
              <span class="title">{{ item.attrName }}</span>
              <div class="box">
                <span v-for="item in item.attrValueList">{{ item }}</span>
              </div>
              <div class="check">
                <div class="check-some">多选</div>
                <div class="more">更多</div>
              </div>
            </div>
            <div class="attr-box showMore" ref="showMoreRef">
              <span class="title">材质</span>
              <div class="box">
                <span>七匹狼</span>
                <span>七匹狼</span>
                <span>七匹狼</span>
                <span>七匹狼</span>
                <span>七匹狼</span>
                <span>七匹狼</span>
                <span>七匹狼</span>
                <span>七匹狼</span>
              </div>
              <div class="check">
                <div class="check-some">多选</div>
                <div class="more" @click="isShowMore">更多</div>
              </div>
            </div>
            <div class="attr-box">
              <span class="title">功能箱包</span>
              <div class="box">
                <span>七匹狼</span>
                <span>七匹狼</span>
                <span>七匹狼</span>
                <span>七匹狼</span>
                <span>七匹狼</span>
                <span>七匹狼</span>
                <span>七匹狼</span>
                <span>七匹狼</span>
              </div>
              <div class="check">
                <div class="check-some">多选</div>
                <div class="more">更多</div>
              </div>
            </div>
            <!-- 下拉框模拟 -->
            <div class="attr-box attrCheck">
              <span class="title">筛选条件</span>
              <div class="box">
                <span>女包</span>
                <span>适用对象</span>
                <span>流行款式</span>
                <span>流行元素</span>
                <span>相关分类</span>
              </div>
              <div class="check">
                <div class="check-some">多选</div>
                <div class="more">更多</div>
              </div>
            </div>

          </div>
          <!-- 伪类active显示容器 -->
          <div class="active-nav" v-if="false">
            <span>小方包</span>
            <span>小方包</span>
            <span>小方包</span>
            <span>小方包</span>
            <span>小方包</span>
            <span>小方包</span>
            <span>小方包</span>
            <span>小方包</span>
            <span>小方包</span>
            <span>小方包</span>
            <span>小方包</span>
            <span>小方包</span>
            <span>小方包</span>
            <span>小方包</span>
            <span>小方包</span>
          </div>
        </div>
        <div class="mainsrp-related">
          <div class="title">宁是不是要找：</div>
          <div class="main">
            <span>行李箱包</span>
            <span>行李箱包</span>
            <span>行李箱包</span>
            <span>行李箱包</span>
            <span>行李箱包</span>
            <span>行李箱包</span>
            <span>行李箱包</span>
          </div>
        </div>
        <div class="mainsrp-sortBar">
          <ul class="sorts">
            <li class="sort">综合排序 </li>
            <li class="sort">销量 </li>
            <li class="sort point" @click="rankXiongYong">信用 </li>
            <li class="sort price-box">
              <div class="price-title">价格</div>
              <div class="price-show">
                <div class="price-show-flex">
                  <span>价格icon</span>
                  <span @click="searchPriceRank(1)">价格从低到高</span>
                  <span @click="searchPriceRank(0)">价格从高到低</span>
                </div>
              </div>

            </li>
          </ul>

          <div></div>
          <div class="prices">
            <input type="text" placeholder="¥" v-model.number="priceSmall" @keyup.enter="priceRange" @blur="priceRange">
            <input type="text" placeholder="¥" v-model.number="priceMax" @keyup.enter="priceRange" @blur="priceRange">
          </div>
          <div class="pager">分页器</div>
          <div class="styles">待开发（列表展示）</div>
          <div class="location">发货地</div>
        </div>
        <div class="mainsrp-itemlist">
          <div class="item-every" v-for="item in rankSincerity">
            <div class="item-every-img"><img :src="item.url" alt=""></div>
            <div class="item-every-body">
              <div class="row-1">
                <div class="price">¥无价</div>
                <div class="deal">信用值：{{ item.sincerity }}</div>
              </div>
              <div class="row-2">
                <span>包邮</span>
                <span>可套拉杆箱上的旅行包女大容量旅游轻便附加登机手</span>
              </div>
              <div class="row-3">
                <div class="store">卢梭旗舰店</div>
                <div class="address">河北&nbsp&nbsp保定</div>
              </div>
              <div class="row-4">
                <div class="">icon</div>
                <div class="slog">小log</div>
              </div>
            </div>
          </div>
          <div @click="toDetail(item)" class="item-every" v-for="item in goodsList">
            <div class="item-every-img"><img :src="item.defaultImg" alt=""></div>
            <div class="item-every-body">
              <div class="row-1">
                <div class="price">¥{{ item.price }}</div>
                <div class="deal">3000+人付款</div>
              </div>
              <div class="row-2">
                <span>包邮</span>
                <span>{{ item.title }}</span>
              </div>
              <div class="row-3">
                <div class="store">卢梭旗舰店</div>
                <div class="address">河北&nbsp&nbsp保定</div>
              </div>
              <div class="row-4">
                <div class="">icon</div>
                <div class="slog">小log</div>
              </div>
            </div>
          </div>
        </div>

        <!-- 分页器占位 -->
        <div class="mainsrp-pager">
        </div>
      </div>
      <div class="grid-right">
        <div class="header">掌柜热卖</div>
        <ul v-for="item in 16">
          <li class="hot-list">
            <div class="hot-list-img">
              <img src="../../assets/12.jpg" alt="">
            </div>
            <div class="hot-list-price">¥299.00</div>
          </li>
        </ul>
      </div>
    </div>

    <!-- 掌柜热卖1000x452 -->
    <div class="grid__hot"></div>
  </div>
</template>

<script lang="ts">
export default {
  name: "GSearch",
};
</script>

<script lang="ts" setup>
// import SearchHeader from '../../components/SearchHeader/index.vue'

import { reqSearchGoodsList } from '@/api/search/search'
import { reqSearchXinYongMock } from '@/api/search/search-mock'

import { onMounted, reactive, ref ,watch} from 'vue';
import { useRouter,useRoute } from 'vue-router';

//创建路由器
const router = useRouter()
//创建路由
const route = useRoute()



/**
 * -1.js显示更多与隐藏更多控制
 */
const showMoreRef = ref()
const flagClass = ref(false)
const isShowMore = () => {
  flagClass.value = !flagClass.value
  if (flagClass.value) {
    showMoreRef.value.classList.remove("showMore")
  } else {
    showMoreRef.value.classList.add("showMore")
  }
}

//定义响应式请求数据
const searchOptions = reactive({
  // 分类和关键字参数直接从路由参数中获取
  // 分类
  // category1Id: undefined,
  // category2Id: undefined,
  // category3Id: undefined,
  // categoryName: "",
  // // 关键字
  keyword: "",
  // 属性
  // '属性id:属性值:属性名'
  props: [],
  // 品牌
  // '品牌id:品牌名称'
  trademark: "",
  // 排序
  // 排序类型：1综合 2价格  排序标识：asc升序 desc降序
  order: "1:desc",
  // 分页
  pageNo: 1,
  pageSize: 100,

})
//定义响应式请求数据
const trademarkList = ref([])
const attrsList = ref([])
const goodsList = ref([])


onMounted(() => {
  // searchOptions.keyword=route.query.searchKeyWord
  // searchGoods()
  rankXiongYong()
})

/**
 * 1获取商品列表
 */
const searchGoods = async () => {

  const res = await reqSearchGoodsList(searchOptions)
  //  console.log(res);
  trademarkList.value = res.trademarkList
  attrsList.value = res.attrsList
  goodsList.value = res.goodsList
}

/**
 * 2.品牌搜素，根据品牌名搜素
 */
const trademarkSearch = (item) => {
  const Newtrademark = `${item.tmId}:${item.tmName}`
  // 解决同一个品牌，就不重复搜索
  if (searchOptions.trademark === Newtrademark)
    return
  searchOptions.trademark = Newtrademark
  searchGoods()
}

/**
 * 3.价格排行搜素
 */
const searchPriceRank = (flag) => {
  // console.log(flag);
  //价格从低到高排序
  if (flag === 1) {
    searchOptions.order = "2:asc"
    searchGoods()
  } else {
    searchOptions.order = "2:desc"
    searchGoods()
  }
}

/**
 * 4.信用排行搜索（模拟）
 */
const rankSincerity = ref([])
const flagSincerity = ref(false)
const rankXiongYong = () => {
  //  reqSearchXinYongMock()根据返回promise如下出来
  reqSearchXinYongMock().then((result) => {
    // console.log(result);根据返回值处理如下
    flagSincerity.value = !flagSincerity.value
    // console.log(flagSincerity.value);
    //排序其实可简写的
    rankSincerity.value = result.data
    rankSincerity.value.sort((a, b) => {
      if (flagSincerity.value) {
        return b.sincerity - a.sincerity
      } else {
        return a.sincerity - b.sincerity
      }
    })
  })
}

/**
 * 5.价格区间搜索
 */
//定义双向价格绑定数据
const priceSmall = ref()
const priceMax = ref()
const priceRange = () => {
  //①当填写最低价和最高价才筛选
  if ((typeof priceSmall.value === 'number') && (typeof priceMax.value === 'number')) {
    //②当用户填反了最小值和最大值要交换数据，再做筛选
    const priceRangeArr = []
    priceRangeArr.push(priceSmall.value)
    priceRangeArr.push(priceMax.value)
    if (priceSmall.value > priceMax.value) {
      //利用ES6的解构赋值能更加便捷的进行元素交换
      [priceRangeArr[0], priceRangeArr[1]] = [priceRangeArr[1], priceRangeArr[0]]
    }
    goodsList.value = goodsList.value.filter((item) => {
      return item.price >= priceRangeArr[0] && item.price <= priceRangeArr[1]
    })
  } else {
    return
  }
}

/**
 * 6.跳转详情页并传参
 */
const toDetail = (item) => {
  // console.log(item.id);
  router.push({
    path: "/Detail",
    query: { skuId: item.id },
  });
};

/**
 * 7.搜索商品，根据关键字搜索监视属性
 */
watch(()=>route.query.keyword
,()=>{
  // 关键字赋值给本页，然后搜索
   searchOptions.keyword=route.query.searchKeyWord
   searchGoods()
},{deep:true,immediate:true}
)

</script>

<style scoped lang="less">
.body {
  margin: 0 auto;
  width: 1200px;
  // background-color: pink;
}

// 搜索头部
.search__header {
  width: 940px;
  height: 60px;
}

.grid-total {
  display: flex;
  border-radius: 30px;
  justify-content: space-between;
  width: 1200px;
  border: 1px solid black;
  padding-top: 20px;


  .grid-left {
    width: 940px;
    height: 4394px;

    .mainsrp-tab {
      width: 940px;
      display: flex;

      .nav-box {
        display: flex;
        color:#000;

        div {
          margin-left: 20px;
          font-size: 18px;
          font-weight: 700;
        }
      }

    }

    .mainsrp-nav {
      position: relative;
      border: 2px solid;
      width: 940px;
      border-top: 2px solid red;
      color:#111111;

      .show-icon {
        display: flex;
        height:30px;
        justify-content: space-between;
      }

      .groups-item {
        display: flex;
        flex-direction: column;

        .attr-box {
          display: flex;
          cursor: pointer;


          .title {
            min-width: 100px;
          }

          .box {
            display: flex;
            flex-wrap: wrap;
            flex: 1;

            span {

              width: 100px;
              height: 40px;
            }

          }

          .check {
            min-width: 100px;
            display: flex;

            .check-some {
              margin-left: 20px;
            }

            .more {
              margin-left: 20px;
            }
          }

        }

        .showMore {
          height: 40px;
        }

        .attrCheck {
          background-color: yellow;

          .box>span:hover {}

        }

      }

      .active-nav {
        // 防止价格筛选出现
        z-index: 99;
        position: absolute;
        display: flex;
        flex-wrap: wrap;
        align-content: space-around;
        background-color: pink;
        width: 940px;

        span {
          width: 100px;
          height: 40px;
          margin: 10px 10px;
        }
      }
    }

    .mainsrp-related {
      display: flex;
      width: 940px;
      border: 1px solid;


      .title {
        min-width: 100px;
        margin: 10px;
      }
    }

    .mainsrp-sortBar {
      width: 940px;
      display: flex;
      justify-content: space-around;
      align-items: center;
      height: 39px;
      background-color: #f5f5f5;
      border: solid 1px #e8e8e8;

      .sorts {
        display: flex;
        justify-content: space-around;

        .sort {
          margin-left: 15px;

          .price-title:hover+.price-show {
            display: block;

          }
        }

        .price-box {
          position: relative;

          // 鼠标移上价格字体显示
          .price-box:hover .price-show {
            display: block;

          }

          // 鼠标移上弹出框去显示
          .price-show {
            &:hover {
              display: block;
            }

            // 鼠标离开隐藏
            display: none;
            position: absolute;
            top: -40px;
            left: -40px;

            .price-show-flex {
              display: flex;
              flex-direction: column;
              justify-content: space-around;
              align-items: center;
              background-color: white;
              width: 120px;
              height: 120px;
              border-radius: 10px;

              :first-child {
                font-size: 16px;
                font-weight: 700;
              }
            }
          }
        }

        .point {
          cursor: pointer;
        }
      }
    }

    .mainsrp-itemlist {

      display: flex;
      flex-wrap: wrap;
      justify-content: space-between;

      .item-every {
        margin-bottom: 20px;

        .item-every-img {
          img {
            width: 220px;
            height: 220px;
            border-radius: 18px
          }

        }

        .item-every-body {
          width: 220px;
          display: flex;
          flex-direction: column;

          .row-1 {
            display: flex;
            justify-content: space-between;

          }

          .row-2 {
            display: flex;
            justify-content: space-between;

          }

          .row-3 {
            display: flex;
            justify-content: space-between;

          }

          .row-4 {
            display: flex;
            justify-content: space-between;

          }

        }

      }

    }
  }

  .grid-right {
    width: 240px;
    .header{
      font-weight: 600;
      font-size: 18px;

    }



    .hot-list-img {
      img {
        border-radius: 30px;
        width: 240px;
        height: 240px;
      }

    }

    .header {
      font-weight: 100;
    }

    .hot-list {
      display: flex;
      flex-direction: column;
      justify-content: space-around;
      margin-top: 10px;
    }
  }



}

.grid__hot {
  width: 1000px;
  height: 452px;
}
</style>
